﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using UIT.Controls.Containers;
using System.Web;
using System.IO;
using System.Reflection;
using System.Xml;
using JQueryControlToolkit;
using UIT.Web.Markup;
using UIT.Controls;
using System.Web.Handlers;

namespace UIT.Web.Renderers
{
    public class PageRenderer : WebControlRenderer<Page>
    {
        static PageRenderer()
        {
            var mi = typeof(AssemblyResourceLoader).GetMethod("GetWebResourceUrl", BindingFlags.Static | BindingFlags.NonPublic, null, new Type[] { typeof(Type), typeof(string), typeof(bool) }, null);
            GetWebResourceUrl = (Func<Type, string, bool, string>)Delegate.CreateDelegate(typeof(Func<Type, string, bool, string>), mi);
        }

        public static readonly Func<Type, string, bool, string> GetWebResourceUrl;

        public override void Render(Page control)
        {

            writer.WriteDocType("html", null, null, null);
            writer.WriteStartElement("html");
            writer.WriteStartElement("head");

            writer.WriteStartElement("link");
            writer.WriteAttributeString("type", "text/css");
            writer.WriteAttributeString("rel", "stylesheet");
            writer.WriteAttributeString("href", GetWebResourceUrl(typeof(PageRenderer), "UIT.Web.UIT.css", false));
            writer.WriteEndElement();

            if (control.Metadata != null && Controls.Page.GetTheme(control) != null)
            {
                writer.WriteStartElement("link");
                writer.WriteAttributeString("type", "text/css");
                writer.WriteAttributeString("rel", "stylesheet");
                writer.WriteAttributeString("href", "http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.17/themes/" + Controls.Page.GetTheme(control) + "/jquery-ui.css");
                writer.WriteEndElement();
            }


            writer.WriteStartElement("script");
            writer.WriteAttributeString("src", "http://code.jquery.com/jquery-1.7.js");
            writer.WriteAttributeString("type", "text/javascript");
            writer.WriteFullEndElement();
            writer.WriteStartElement("script");
            writer.WriteAttributeString("src", "http://ajax.aspnetcdn.com/ajax/jquery.ui/1.8.14/jquery-ui.min.js");
            writer.WriteAttributeString("type", "text/javascript");
            writer.WriteFullEndElement();
            writer.WriteStartElement("script");
            writer.WriteAttributeString("src", "http://ajax.aspnetcdn.com/ajax/jquery.validate/1.8.1/jquery.validate.min.js");
            writer.WriteAttributeString("type", "text/javascript");
            writer.WriteFullEndElement();

            writer.WriteStartElement("script");
            writer.WriteAttributeString("src", "http://ajax.aspnetcdn.com/ajax/modernizr/modernizr-2.0.6-development-only.js");
            writer.WriteAttributeString("type", "text/javascript");
            writer.WriteFullEndElement();

            writer.WriteStartElement("script");
            writer.WriteAttributeString("src", "http://ajax.aspnetcdn.com/ajax/jquery.cycle/2.99/jquery.cycle.all.min.js");
            writer.WriteAttributeString("type", "text/javascript");
            writer.WriteFullEndElement();

            writer.WriteStartElement("script");
            writer.WriteAttributeString("src", "http://ajax.aspnetcdn.com/ajax/jquery.templates/beta1/jquery.tmpl.min.js");
            writer.WriteAttributeString("type", "text/javascript");
            writer.WriteFullEndElement();

            writer.WriteStartElement("script");
            writer.WriteAttributeString("type", "text/javascript");
            writer.WriteAttributeString("src", ResolveUrl.Process("~/UIT.jl"));
            writer.WriteFullEndElement();

            writer.WriteStartElement("script");
            writer.WriteAttributeString("type", "text/javascript");
            writer.WriteAttributeString("src", GetWebResourceUrl(typeof(PageRenderer), "UIT.Web.UIT.js", false));
            writer.WriteFullEndElement();

            writer.WriteStartElement("script");
            writer.WriteAttributeString("type", "text/javascript");
            writer.WriteAttributeString("src", GetWebResourceUrl(typeof(PageRenderer), "UIT.Web.jCoB.js", false));
            writer.WriteFullEndElement();

            writer.WriteStartElement("script");
            writer.WriteAttributeString("type", "text/javascript");
            writer.WriteAttributeString("src", GetWebResourceUrl(typeof(PageRenderer), "UIT.Web.jAcl.js", false));
            writer.WriteFullEndElement();

            writer.WriteStartElement("script");
            writer.WriteAttributeString("type", "text/javascript");
            writer.WriteAttributeString("src", GetWebResourceUrl(typeof(PageRenderer), "UIT.Web.UIT.Binding.js", false));
            writer.WriteFullEndElement();

            writer.WriteStartElement("script");
            writer.WriteAttributeString("type", "text/javascript");
            writer.WriteAttributeString("src", GetWebResourceUrl(typeof(PageRenderer), "UIT.Web.UIT.Template.js", false));
            writer.WriteFullEndElement();

            writer.WriteStartElement("title");
            writer.WriteString(control.Title);
            writer.WriteFullEndElement();

            if (control.Metadata != null)
            {
                foreach (UIT.Controls.IControl item in Web.Controls.Page.GetHead(control))
                {
                    Manager.Render(item);
                }
            }

            writer.WriteEndElement();

            writer.WriteStartElement("body");
            control.RenderFullStyle(this.writer);
            foreach (IControl child in control.Children)
            {
                Manager.Render(child);
            }

            writer.WriteStartElement("script");
            writer.WriteAttributeString("type", "text/javascript");
            foreach (DomResult script in ScriptManager.RegisteredScripts.Values)
            {
                writer.WriteString(script.ToScript());
            }
            writer.WriteEndElement();
            writer.WriteEndElement();
            writer.WriteEndElement();
        }
    }
}
